﻿namespace ImageDupFinder
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using ImageDupFinder.ProgramCore;

    /// <summary>
    /// Picturebox with thumbnail image
    /// </summary>
    internal class ThumbnailPictureBox : PictureBox, ILazyLoad
    {
        /// <summary>
        /// ImageData associated with this picture box
        /// </summary>
        private ImageData img;

        /// <summary>
        /// Flag. States if picturebox is selected
        /// </summary>
        private bool selected = false;

        /// <summary>
        /// set to true if lazy load can be performed, set to false after load.
        /// </summary>
        private bool canLazyLoad = true;

        /// <summary>
        /// Initializes a new instance of the ThumbnailPictureBox class.
        /// </summary>
        /// <param name="img">ImageData associated with this picture box</param>
        public ThumbnailPictureBox(ImageData img)
        {
            this.img = img;
        }

        /// <summary>
        /// Callback function called when lazy loading is initializated.
        /// </summary>
        public event EventHandler LazyLoadCallback;

        /// <summary>
        /// Gets a value indicating whether lazy loading was performed.
        /// </summary>
        public bool CanLazyLoad
        {
            get { return canLazyLoad; }
        }

        /// <summary>
        /// Gets or sets User data that can be used during lazy loading.
        /// Get returns ImageData of this PictureBox.
        /// Set is not implemented.
        /// </summary>
        public object LazyData
        {
            get { return this.ImageData; }
            set { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets Image data associated with picturebox.
        /// </summary>
        public ImageData ImageData
        {
            get { return img; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the picture box has been selected.
        /// </summary>
        public bool Selected
        {
            get
            {
                return selected;
            }

            set
            {
                selected = value;
                if (selected)
                {
                    this.BackColor = Color.Red;
                }
                else
                {
                    this.BackColor = System.Drawing.SystemColors.Control;
                }
            }
        }

        /// <summary>
        /// Invokes lazy loading process.
        /// </summary>
        public void InvokeLazyLoad()
        {
            if (canLazyLoad)
            {
                canLazyLoad = false;

                EventHandler tmp = this.LazyLoadCallback;
                if (tmp != null)
                {
                    LazyLoadCallback(this, null);
                }
            }
        }
    }
}
